package com.alan.sso.server.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alan.sso.server.bean.AppConfig;
import com.alan.sso.server.bean.User;
import com.alan.sso.server.service.ILoginServer;
import com.alan.sso.share.bean.SsoUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: Alan liu
 * @Date: 2018/12/21 13:08
 * @Description: 简易实现
 */
@Service
public class LoginServerImpl implements ILoginServer {

    private static List<AppConfig> appConfigList = new ArrayList<>();
    private static List<User> userList = new ArrayList<>();
    /**
     * 模拟redis，这里不做有效期。
     */
    private Map<String,User> ticketCacheMap = new HashMap<>();
    private Map<Long,String> uidForTicketMap = new HashMap<>();

    static {
        //init app data
        AppConfig appConfig1 = new AppConfig();
        appConfig1.setAppName("sso-client");
        appConfig1.setAppKey("wefwerfwerwr1415efefetdfetfefedf");
        appConfigList.add(appConfig1);
        AppConfig appConfig2 = new AppConfig();
        appConfig2.setAppName("sso-alan");
        appConfig2.setAppKey("wefwerfwerwr1415efefetdfetfef254");
        appConfigList.add(appConfig2);
        //init user data
        User user1 = new User();
        user1.setId(1L);
        user1.setUserName("alan");
        user1.setPassword("123456");
        userList.add(user1);
    }


    @Override
    public boolean checkTicket(String ticket) {
        User user = ticketCacheMap.get(ticket);
        if(user != null && StringUtils.isNotEmpty(user.getUserName())){
            return true;
        }
        return false;
    }

    @Override
    public User getUserByUserName(String userName) {
        for (User item:userList) {
            if(item.getUserName().equals(userName)){
                return item;
            }
        }
        return null;
    }

    @Override
    public String generateTicket(User user) {
        String ticket = IdUtil.fastUUID();
        ticketCacheMap.put(ticket,user);
        uidForTicketMap.put(user.getId(),ticket);
        return ticket;
    }

    @Override
    public SsoUser getSsoUserByTicket(String ticket) {
        User user = ticketCacheMap.get(ticket);
        if( user != null ){
            SsoUser ssoUser = new SsoUser();
            BeanUtils.copyProperties(user,ssoUser);
            return ssoUser;
        }
        return null;
    }

    @Override
    public boolean logout(SsoUser ssoUser) {
        String ticket = uidForTicketMap.get(ssoUser.getId());
        ticketCacheMap.remove(ticket);
        uidForTicketMap.remove(ssoUser.getId());
        return false;
    }

    @Override
    public AppConfig getAppConfig(String appName) {
        for (AppConfig item:appConfigList) {
            if(item.getAppName().equals(appName)){
                return item;
            }
        }
        return null;
    }


}
